<?php

class Reale_Model_Estate_PropertyMapper
{	
	/**
     * @var Zend_Db_Table_Abstract
     */
    protected $_dbTable;
    

    /**
     * Specify Zend_Db_Table instance to use for data operations
     * 
     * @param  Zend_Db_Table_Abstract $dbTable 
     * @return Reale_Model_Estate_PropertyMapper
     */
    public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    /**
     * Get registered Zend_Db_Table instance
     *
     * Lazy loads Reale_Model_DbTable_Property if no instance registered
     * 
     * @return Zend_Db_Table_Abstract
     */
    public function getDbTable()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('Reale_Model_DbTable_Property');
        }
        return $this->_dbTable;
    }
    
    
    /**
     * 
     * To save new or update existing agent's informtion.
     * @param Reale_Model_Estate_Property
     * @return Reale_Model_Estate_Property | null 
     */
    public function save(Reale_Model_Estate_Property &$property){
    	$commonData = array(
    			'category_id'=>$property->getCategory_id(),
    			'type_id'=>$property->getType_id(),
                'city'=>$property->getCity(),
                'count'=>$property->getCount(),
                'location'=>$property->getLocation(),
                'bhk'=>$property->getBhk(),
                'rate'=>$property->getRate(),
                'size'=>$property->getSize(),
    			'size_units'=>$property->getSize_units(),
                'state'=>$property->getState(),
                'summary'=>$property->getSummary());
        $id = $property->getEstate_id();
        if ($id) {
            $rowsUpdated = $this->getDbTable()->update(
            array('estate_id = ?' => $id));
            return $rowsUpdated;
        } else {
            $estateTable = new Reale_Model_DbTable_Estate();
            try {
                $this->getDbTable()
                    ->getAdapter()
                    ->beginTransaction();
                $pKey = $estateTable->insert($commonData);
                $property->setEstate_id($pKey['estate_id']);
                
                $propData = array('property_id' => $property->getEstate_id(), 
                'owner' => $property->getOwner(), 
                'owner_address' => $property->getOwner_address(), 
                'owner_contact_no' => $property->getOwner_contact_no());
                
                $this->getDbTable()->insert($propData);
                $this->getDbTable()
                    ->getAdapter()
                    ->commit();
            } catch (Zend_Exception $e) {
                $this->getDbTable()
                    ->getAdapter()
                    ->rollBack();
                $logger = new Zend_Registry('logger');
                $logger->debug($e);
                throw $e;
            }
            return $property;
        }
        
        return null;
    }
	
}

